<script>
$(document).ready(function () {

    $("#chooseicon>ul>li").click(function () {
        var fa_icon = $(this).attr("data-font");
        $("#IconModal").modal("hide");
        $("#menu-icon").val("fa-"+fa_icon)
        $("#edit-menu-icon").val("fa-"+fa_icon)
    });

    $("input[name='is-menu']").click(function () {
        $("input[name='is-menu'][checked]").removeAttr("checked");
        $(this).attr("checked","checked")
    });

    $("input[name='menu-status']").click(function () {
        $("input[name='menu-status'][checked]").removeAttr("checked");
        $(this).attr("checked","checked")
    });


    $("input[name='edit-is-menu']").click(function () {
        $("input[name='edit-is-menu'][checked]").removeAttr("checked");
        $(this).attr("checked","checked")
    });

    $("input[name='edit-menu-status']").click(function () {
        $("input[name='edit-menu-status'][checked]").removeAttr("checked");
        $(this).attr("checked","checked")
    });


    //显示隐藏菜单url输入框
    $("#p-menu").change(function () {
        var p_id = $(this).val();
        if( p_id != 0){
            $("#div-url").show()
        }else{
            $("#div-url").hide()
        }

    });

    //显示隐藏菜单url输入框
    $("#edit-p-menu").change(function () {
        var p_id = $(this).val();
        if( p_id != 0){
            $("#edit-div-url").show()
        }else{
            $("#edit-div-url").hide()
        }

    });


    //加载页面时隐藏功能菜单
    $("tbody>tr").each(function () {
       var m_status = $(this).attr("status");
       if (m_status == 'false'){
           this.style.display = 'none';
       }
    });

    //显示隐藏功能菜单
    $("a[name='show-child']").click(function () {
        var m_id = $(this).attr("menu_id");
        $("tr[name='child-tr']").each(function () {
            var pm_id = $(this).attr("pmenu");
            if (pm_id == m_id){
                if (this.style.display == 'none'){
                    this.style.display = 'table-row';
                }else{
                    this.style.display = 'none';
                }
            }
        });
    });

    //添加菜单
    $("#add-menu-sub").click(function () {
        var is_menu = $("input[name='is-menu'][checked]").val();
        var p_menu = $("#p-menu").val();
        var menu_title = $("#menu-title").val();
        var menu_icon = $("#menu-icon").val();
        var menu_url = $("#menu-url").val();
        var menu_weight = $("#menu-weight").val();
        var menu_status = $("input[name='menu-status'][checked]").val();
        $.ajax({
            type:"POST",
            url:"/rbac/addmenu/",
            data:{"is_menu":is_menu,"p_menu":p_menu,"menu_title":menu_title,"menu_icon":menu_icon,
                "menu_url":menu_url,"menu_weight":menu_weight,"menu_status":menu_status},
            dataType: "json",
            success:function (data) {
                $("#addMenuModal").modal("hide");
                spop({
                    template: data,
                    style:'success',
                    autoclose:2000
                });
                setTimeout("location.reload()",2100)
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    $("td>a[name='edit-menu']").click(function () {
        var m_id = $(this).attr("id");
        $.ajax({
            type: "POST",
            url:"/rbac/idmenu/",
            data: {"m_id":m_id},
            dataType: "json",
            success:function (data) {
                $("input[name='edit-is-menu']").removeAttr("checked");
                if(data.IsMenu){
                    $("input[name='edit-is-menu'][value='true']").attr("checked","checked");
                }else{
                    $("input[name='edit-is-menu'][value='false']").attr('checked','checked');
                }

                $("input[name='edit-menu-status']").removeAttr("checked");
                if(data.Status){
                    $("input[name='edit-menu-status'][value='true']").attr("checked","checked");
                }else{
                    $("input[name='edit-menu-status'][value='false']").attr('checked','checked');
                }

                $("#edit-p-menu").val(data.PMenu);
                $("#edit-menu-title").val(data.MenuTitle);
                $("#edit-menu-icon").val(data.MenuIcon);
                $("#edit-menu-url").val(data.MenuUrl);
                $("#edit-menu-weight").val(data.MenuWeight);
                $("#edit-menu-sub").attr("id",data.ID);
                $("#editMenuModal").modal("show");
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    $("#edit-menu-sub").click(function () {
        var is_menu = $("input[name='edit-is-menu'][checked]").val();
        var p_menu = $("#edit-p-menu").val();
        var menu_title = $("#edit-menu-title").val();
        var menu_icon = $("#edit-menu-icon").val();
        var menu_url = $("#edit-menu-url").val();
        var menu_weight = $("#edit-menu-weight").val();
        var menu_status = $("input[name='edit-menu-status'][checked]").val();
        var m_id = $(this).attr("id");
        $.ajax({
            type:"PUT",
            url:"/rbac/editmenu/",
            data:{"m_id":m_id,"is_menu":is_menu,"p_menu":p_menu,"menu_title":menu_title,"menu_icon":menu_icon,
                "menu_url":menu_url,"menu_weight":menu_weight,"menu_status":menu_status},
            dataType: "json",
            success:function (data) {
                $("#editMenuModal").modal("hide");
                spop({
                    template: data,
                    style:'success',
                    autoclose:2000
                });
                setTimeout("location.reload()",2100)
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });
    $("td>a[name='del-menu']").click(function () {
        var m_id = $(this).attr('id');
        var statu = confirm("是否确认删除!");
        if (statu==true)
        {
            $.ajax({
                type: "post",
                async:false,
                url: "/rbac/delmenu/",
                data: {"m_id":m_id},
                dataType : "json",
                success: function(data) {
                    spop({
                        template:data,
                        style: 'warning',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        }
    });

    //搜索角色
    $("#search-menu").bind('change',function () {
        var key = $(this).val();
        if (key != "" ) {
            var url = "/rbac/searchmenu/" + key + "/";

        }else{
            var url = "/rbac/menu/";
        }
        $("#search-menu-a").attr("href", url);
    });

})


</script>
